package com.xja.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xja.model.entity.Feedback;
import com.xja.model.vo.resp.Result;
import com.xja.service.IFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户反馈表 前端控制器
 * </p>
 *
 * @author caijg
 * @since 2025-06-09
 */
@RestController
@RequestMapping("/admin/feedback")
@Slf4j
@Tag(name = "用户反馈管理")
@RequiredArgsConstructor
public class AdminFeedbackController {

    private final IFeedbackService feedbackService;

    @GetMapping("/list")
    @Operation(summary = "获取反馈列表")
    public Result<Page<Feedback>> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String content,
            @RequestParam(required = false) Integer status) {
        log.info("获取反馈列表：page = {}, size = {}, content = {}", page, size, content);

        // 构建查询条件
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(content)) {
            wrapper.like(Feedback::getContent, content);
        }
        wrapper.orderByDesc(Feedback::getCreateTime);
        // 筛选条件（已查看/未查看）
        if (status != null) {
            wrapper.eq(Feedback::getStatus, status);
        }

        // 分页查询
        Page<Feedback> pageResult = feedbackService.page(
                new Page<>(page, size),
                wrapper
        );

        return Result.success(pageResult);
    }

    @PutMapping("/status/{id}")
    @Operation(summary = "更新反馈状态")
    public Result<Void> updateStatus(
            @PathVariable Long id,
            @RequestBody Map<String, Integer> params) {
        log.info("更新反馈状态：id = {}, status = {}", id, params.get("status"));

        boolean success = feedbackService.update()
                .eq("id", id)
                .set("status", params.get("status"))
                .update();

        return success ? Result.success() : Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除反馈")
    public Result<Void> delete(@PathVariable Long id) {
        log.info("删除反馈：id = {}", id);

        boolean success = feedbackService.removeById(id);
        return success ? Result.success() : Result.error("删除失败");
    }
}
